package UILayer;

import CtrLayer.LeaseCtr;
import ModelLayer.CurrentAccount;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;



public class MachinesGUI extends JInternalFrame
{
	private JTextField text_field;
	private LeaseCtr machine_ctr;
	private JTable product_table;
	JButton btnNewButton;
	private String[] column_names = { "ID", "Name", "Price", "Available", "Finished"};
	


	public MachinesGUI()
	{
		super("Machines", true, true, true, true);
		this.addInternalFrameListener(new InternalFrameAdapter() { public void internalFrameActivated(InternalFrameEvent e) { refresh(); } });
		machine_ctr = new LeaseCtr();
		fill_frame();
	}
	
	private void refresh()
	{
			refill_full_table();
	}
	private void refill_full_table()
	{
		Object[][] filling = machine_ctr.display_all_machines();
		product_table.setModel(new MyTableModel(filling, column_names));
	}
	
	private void make_table()
	{
		
		final Object[][] filling = machine_ctr.display_all_machines();
		
		product_table = new JTable() ;

		product_table.setFillsViewportHeight(true);
		product_table.setModel(new MyTableModel(filling, column_names)); 
		product_table.setPreferredScrollableViewportSize(new Dimension(400, 70));
		product_table.setAutoCreateRowSorter(true);
		product_table.setRowSelectionAllowed(true);
		product_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		product_table.getColumnModel().getColumn(4).setMinWidth(160);
		product_table.getTableHeader().setReorderingAllowed(false);
		product_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment( JLabel.RIGHT );
		product_table.setDefaultRenderer(String.class, right_renderer);
		
		JScrollPane scroll_pane = new JScrollPane(product_table);
		scroll_pane.setBorder(new EmptyBorder(5,10,5,10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
		product_table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
	        public void valueChanged(ListSelectionEvent event) {
	            if ((!(CurrentAccount.give_rank().equals("employee")))&&(product_table.getSelectedRow()!=-1)&&(product_table.getValueAt(product_table.getSelectedRow(), 4).toString().equals("Unfinished")))
	            {
	            	btnNewButton.setVisible(true);
	            }
	            else
	            {
	            	btnNewButton.setVisible(false);
	            }
	        }
	    });
	}
	
	private void make_ctr_buttons()
	{
		JPanel ctr_btn_panel = new JPanel();
		ctr_btn_panel.setBorder(new EmptyBorder(3,3,0,3));
		
		getContentPane().add(ctr_btn_panel, BorderLayout.SOUTH);
		GridBagLayout gbl_ctr_btn_panel = new GridBagLayout();
		gbl_ctr_btn_panel.columnWidths = new int[]{0, 0, 0, 70, 0};
		gbl_ctr_btn_panel.rowHeights = new int[]{0, 0, 0};
		gbl_ctr_btn_panel.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_ctr_btn_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		ctr_btn_panel.setLayout(gbl_ctr_btn_panel);
		
		JButton btn_new = new JButton("New");
		GridBagConstraints gbc_btn_new = new GridBagConstraints();
		gbc_btn_new.anchor = GridBagConstraints.EAST;
		gbc_btn_new.insets = new Insets(0, 0, 5, 5);
		gbc_btn_new.gridx = 0;
		gbc_btn_new.gridy = 0;
		btn_new.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { add(); } });
		ctr_btn_panel.add(btn_new, gbc_btn_new);
		
		JButton btn_edit = new JButton("Edit");
		GridBagConstraints gbc_btn_edit = new GridBagConstraints();
		gbc_btn_edit.anchor = GridBagConstraints.NORTHEAST;
		gbc_btn_edit.insets = new Insets(0, 0, 5, 5);
		gbc_btn_edit.gridx = 2;
		gbc_btn_edit.gridy = 0;
		btn_edit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { edit(); } });

		btnNewButton = new JButton("Complete");
		btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { complete();}});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.gridx = 1;
		gbc_btnNewButton.gridy = 0;
		ctr_btn_panel.add(btnNewButton, gbc_btnNewButton);
		ctr_btn_panel.add(btn_edit, gbc_btn_edit);
		btnNewButton.setVisible(false);
		
		JButton btn_del = new JButton("Delete");
		GridBagConstraints gbc_btn_del = new GridBagConstraints();
		gbc_btn_del.insets = new Insets(0, 0, 5, 0);
		gbc_btn_del.anchor = GridBagConstraints.WEST;
		gbc_btn_del.gridx = 3;
		gbc_btn_del.gridy = 0;
		btn_del.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { delete(); } });
		ctr_btn_panel.add(btn_del, gbc_btn_del);
	}
	
	private void make_search(JPanel panel)
	{
		JPanel search_panel = new JPanel();
		panel.add(search_panel, BorderLayout.WEST);
		search_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblNewLabel = new JLabel("Name/ID");
		search_panel.add(lblNewLabel);
		
		text_field = new JTextField();
		search_panel.add(text_field);
		text_field.setColumns(20);
		
		JButton btn_search = new JButton("Search");
		btn_search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { search(); } });
		search_panel.add(btn_search);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { refresh(); } });
		search_panel.add(btnClear);
		
	}
	
	private void add()
	{
		MachineManager p = new MachineManager(null,false);
		getDesktopPane().add(p);
		Point current_win = getLocationOnScreen();
		int x = (int)current_win.getX() + 20;
		int y = (int) current_win.getY() + 5;
		p.setLocation(x,y);
		p.setVisible(true);
	}
	
	private void edit()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			MachineManager p = new MachineManager(String.valueOf( product_table.getModel().getValueAt(row_index, 0)),false);
			getDesktopPane().add(p);
			Point current_win = getLocationOnScreen();
			int x = (int)current_win.getX() + 20;
			int y = (int) current_win.getY() + 5;
			p.setLocation(x,y);
			p.setVisible(true);
		}
	}
	private void complete()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			MachineManager p = new MachineManager(String.valueOf( product_table.getModel().getValueAt(row_index, 0)),true);
			getDesktopPane().add(p);
			Point current_win = getLocationOnScreen();
			int x = (int)current_win.getX() + 20;
			int y = (int) current_win.getY() + 5;
			p.setLocation(x,y);
			p.setVisible(true);
		}
	}
	
	
	private void delete()
	{
		int row_index = product_table.getSelectedRow();
		if (row_index != -1)
		{
			if (!String.valueOf(product_table.getModel().getValueAt(row_index, 3)).equals("Unavailable"))
			{
				if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this product?", "Delete product", JOptionPane.YES_NO_OPTION))
				{
					machine_ctr.delete_machine((String) product_table.getModel().getValueAt(row_index, 0));
					refresh();
				}
			}
		}
		
	}
	
	private void search()
	{
		Object[][] filling = new Object[1][5];
		String contents = text_field.getText();
			if (is_numeric(contents))
			{
				if (machine_ctr.is_such_machine_id(contents))
				{
					filling[0] = machine_ctr.display_machine_id(contents);	
				}
				else 
				{
					filling= null;
				}
			}
			else
			{
				if (machine_ctr.is_such_machine_name(contents))
				{
					filling[0] = machine_ctr.display_machine_name(contents);	
				}
				else 
				{
					filling= null;
				}
			}
		product_table.setModel(new MyTableModel(filling, column_names));
	
	}
	
	private void fill_frame()
	{
		setBounds(100, 100, 414, 290);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		make_search(panel);
		make_ctr_buttons();
		make_table();
	}
	
	
	private boolean is_numeric(String checker)
	{
		 try  
		  {  
		    int i = Integer.parseInt(checker);  
		  }  
		  catch(NumberFormatException nfe)  
		  {  
		    return false;  
		  }  
		  return true;  
	}
	
	
	 
	

}
